import 'package:auto_size_text/auto_size_text.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:gongge/utils/AudioUtils.dart';
import '../config/AppConst.dart';
import '../generated/l10n.dart';
import '../help/ClassicesHelp.dart';
import '../utils/CommUtils.dart';
import '../utils/SpUtils.dart';
import '../livedata/LanguageProvider.dart';
import '../pages/GamePage.dart';
import '../routers/router.dart';
import 'My.dart';

//经典首页
class classicsWidget extends StatefulWidget {
  classicsWidget({Key? key}) : super(key: key);
  NewGameProvider? countModel;
  @override
  State<classicsWidget> createState() => _MyWidgetState()..model = countModel;
}

class _MyWidgetState extends State<classicsWidget> {
  NewGameProvider? model;
  @override
  Widget build(BuildContext context) {
    return getMiddle2();
  }

  Widget getMiddle2() {
    double wight = CommUtils.getWidth(context);
    var lastScoreTime = SpUtils?.getInstance()?.getInt(AppConst.lastScoreTime);
    var dateTime = DateTime.now();
     double hight=CommUtils.getHeight(context);
    return Padding(
      //背景里底部和设置的高度
      padding: EdgeInsets.only(
        top: hight * 0.01,
        bottom: hight * 0.03,
      ),
      child: Container(
        width: double.infinity,
        decoration: BoxDecoration(
          image: DecorationImage(
            image: AssetImage('assets/images/bg_classic.png'),
            fit: BoxFit.fill,
          ),
        ),
        child: ListView(
          children: [
            Padding(
              padding: EdgeInsets.only(
                  left: wight*0.03,
                  right:wight*0.03,
                  top: hight*0.05,
                  bottom: hight*0.01),
              child: Column(
                children: [
                  getItem(
                      "assets/images/trophy1.png",
                      S.current.today_best,
                      ClassicesHelp.getToday(lastScoreTime, dateTime),
                      12,
                      13,
                      19.5,
                      23.5),
                  getLine(hight),
                  getItem(
                      "assets/images/trophy2.png",
                      S.current.week_best,
                      ClassicesHelp.getWeekday(lastScoreTime, dateTime),
                      15,
                      15,
                      18,
                      22),
                  getLine(hight),
                  getItem(
                      "assets/images/trophy3.png",
                      S.current.moth_best,
                      ClassicesHelp.getMonth(lastScoreTime, dateTime),
                      18,
                      18,
                      17,
                      21),
                  getLine(hight),
                  getItem(
                      "assets/images/trophy4.png",
                      S.current.year_best,
                      ClassicesHelp.getyear(lastScoreTime, dateTime),
                      21,
                      21,
                      16,
                      19.5),
                  getLine(hight),
                  Container(
                    decoration: BoxDecoration(
                      image: DecorationImage(
                        image: AssetImage('assets/images/line2.png'),
                        fit: BoxFit.fill,
                      ),
                    ),
                    child: Padding(
                      padding: EdgeInsets.only(
                          top: hight*0.02,
                          bottom: hight*0.02),
                      child: getItem(
                          "assets/images/trophy5.png",
                          S.current.histroy_higt,
                          ClassicesHelp.getHistory(lastScoreTime, dateTime),
                          25,
                          19,
                          15,
                          18),
                    ),
                  ),

                  //继续
                  getcontinueGame(null, wight),
                  SizedBox(
                    height: hight * 0.04,
                  ),
                  GestureDetector(
                    onTap: () {
                      AudioUtils.getInstance()?.play2("start_game.mp3");

                      AudioUtils.getInstance()?.stopHomeBGm();
                      //新游戏
                      startPageDown2(
                        context,
                        GamePage(
                          isContinue: false,
                        ),
                        calllBack: (selectIndex) => setState(() {
                          iscontinueGame = true;
                        }),
                      );
                    },
                    child: Container(
                          width: wight * 0.5,
                          height: hight* 0.06,
                        decoration: BoxDecoration(
                          image: DecorationImage(
                            image: AssetImage('assets/images/continue.png'),
                            fit: BoxFit.fill,
                          ),
                        ),
                        alignment: Alignment.center,
                        child: Padding(
                          padding: EdgeInsets.all(5),
                          child: CommUtils.getTextGame(
                            context,
                            15,
                            S.current.new_game,
                            colorList[0],
                            colorList[1],
                          ),
                        )),
                  ), //
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  bool iscontinueGame = false;

  Widget getcontinueGame(NewGameProvider? model, double wight) {
    double hight=CommUtils.getHeight(context);
    String? continueGame=
   SpUtils.getInstance()?.getString(AppConst.continueGame);
    if (continueGame != null || iscontinueGame) {
      return Column(
        children: [
          SizedBox(
            height: ScreenUtil().setHeight(wight * 0.07),
          ),
          GestureDetector(
            onTap: () {
              AudioUtils.getInstance()?.play2("start_game.mp3");
              AudioUtils.getInstance()?.pause();
              //继续游戏
              startPageDown2(
                context,
                GamePage(
                  isContinue: true,
                ),
                calllBack: (selectIndex) => setState(() {
                  print("hah刷新");
                }),
              );
            },
            child: Container(
                width: wight * 0.5,
                height: hight* 0.06,
                decoration: BoxDecoration(
                  image: DecorationImage(
                    image: AssetImage('assets/images/new_game2.png'),
                    fit: BoxFit.fill,
                  ),
                ),
                alignment: Alignment.center,
                child: Padding(
                  padding: EdgeInsets.only(top: 5, bottom: 5),
                  child: CommUtils.getTextGame(
                    context,
                    15,
                    S.of(context).continueto_,
                    colorList[0],
                    colorList[1],
                  ),
                )),
          ),
        ],
      );
    } else {
      return Text("");
    }
  }

  Widget getLine(double hight) {
    return Column(
      children: [
        SizedBox(
          height: hight*0.02,
        ),
        Container(
          // height: 2,
          width: MediaQuery.of(context).size.width,
          child: Image.asset("assets/images/line.png", fit: BoxFit.fill),
        ),
        SizedBox(
          height:  hight*0.03,
        ),
      ],
    );
  }

  Widget getItem(String img, String text, String score, double widths,
      double hights, double left, double textLeft) {
    return Row(
      children: [
        SizedBox(
          width: ScreenUtil().setWidth(left),
        ),
        Image.asset(
          img,
          fit: BoxFit.contain,
          width: widths,
          height: hights,
        ),
        SizedBox(
          width: ScreenUtil().setWidth(textLeft),
        ),
        AutoSizeText(
          text,
          style: TextStyle(
            fontSize: ScreenUtil().setSp(14),
            fontWeight: FontWeight.bold,
            color: Color.fromRGBO(255, 255, 255, 1),
            height: 0.7,
          ),
          maxLines: 2,
          textAlign: TextAlign.center,
          overflow: TextOverflow.ellipsis,
        ),
        Expanded(child: SizedBox()),
        AutoSizeText(
          score,
          style: TextStyle(
            fontSize: ScreenUtil().setSp(14),
            fontWeight: FontWeight.bold,
            color: Color.fromRGBO(255, 255, 255, 1),
            height: 0.7,
          ),
          maxLines: 1,
          textAlign: TextAlign.center,
          overflow: TextOverflow.ellipsis,
        ),
        SizedBox(
          width: ScreenUtil().setWidth(16.5),
        ),
      ],
    );
  }
}
